package com.example.administrator.youlu;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;

import com.example.administrator.youlu.adapter.MyFragmentPagerAdapter;
import com.example.administrator.youlu.fragment.CalllogFragment;
import com.example.administrator.youlu.fragment.CaotactFragment;
import com.example.administrator.youlu.fragment.DialpadFragment;
import com.example.administrator.youlu.fragment.SmstFragment;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.viewGroup)
    ViewPager viewGroup;
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        // viewpager 板顶适配器
        MyFragmentPagerAdapter adapter =new MyFragmentPagerAdapter(getSupportFragmentManager());

        adapter.addFragment(new CalllogFragment());
        adapter.addFragment(new CaotactFragment());
        adapter.addFragment(new SmstFragment());
        adapter.addFragment(new DialpadFragment());
        viewGroup.setAdapter(adapter);

        viewGroup.setCurrentItem(1);
        viewGroup.setOffscreenPageLimit(3);
        //viewpager 切换监听 rg状态改变
        viewGroup.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                ((RadioButton) radioGroup.getChildAt(i)).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch(i){
                    case R.id.radio1:
                        viewGroup.setCurrentItem(0);
                        break;
                    case R.id.radio2:
                        viewGroup.setCurrentItem(1);
                        break;

                    case R.id.radio3:
                        viewGroup.setCurrentItem(2);
                        break;
                    case R.id.radio4:
                        viewGroup.setCurrentItem(3);
                        break;

                }


            }
        });
        //rg 切换监听 viewPager 改变

    }
}
